package com.sangfor.ssl.service.auth;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.kevinsawicki.http.HttpRequest;
import com.sangfor.ssl.IVpnDelegate;
import com.sangfor.ssl.SangforAuth;
import com.sangfor.ssl.common.Foreground;
import com.sangfor.ssl.common.ScheduleTimer;
import com.sangfor.ssl.common.Values;
import com.sangfor.ssl.l3vpn.service.L3vpnController;
import com.sangfor.ssl.l3vpn.service.LogoutTaskState;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.utils.IGeneral;
import java.io.File;
import org.codehaus.jackson.util.MinimalPrettyPrinter;

/* loaded from: classes2.dex */
public final class AuthorManager {
    public static final int AUTHOR_AUTH_NONE = -5;
    private static final int AUTHOR_COUNT_DEFAULT = 3;
    public static final int AUTHOR_ERROR = 0;
    public static final int AUTHOR_FREZIE = 2;
    public static final int AUTHOR_NET_ERROR = -4;
    public static final int AUTHOR_NET_FAKE_ERROR = -7;
    public static final int AUTHOR_PARA_ERROR = -3;
    public static final int AUTHOR_RANDOM_ERROR = -6;
    public static final int AUTHOR_RANK_ERROR = -2;
    public static final int AUTHOR_SING_ERROR = -1;
    public static final int AUTHOR_SUCESS = 1;
    private static final int LOGOUT_DELAY_TIME = 0;
    private static final int MSG_CHECK_RET = 0;
    private static final int NETWORK_DELAY = 300000;
    private static final int OFFLINE_DELAY = 60000;
    public static final String PUBKEY_SVPN_FILE = "emm_pubkey_svpn.enc.key";
    public static final String PUBKEY_SVPN_FILE_UPDATE_URL = "/com/emm_pubkey_svpn.enc.key";
    public static final String PUBKEY_SXF_FILE = "emm_pubkey_sxf.key";
    private static final int RANK_ADIVICE = 3;
    private static final int RANK_BASIC = 1;
    private static final int RANK_NORAML = 2;
    private static final int SERVER_CONNECT_TIMEOUT = 30;
    private static final String TAG = "AuthorManager";
    private static final int TIMER_DELAY_TIME = 0;
    private final ScheduleTimer.ScheduleTimerTask SCHEDULE_TIMERTASK;
    private Foreground.Listener foregroundListener;
    private Context mAppContext;
    private long mAuthorInterval;
    private int mAuthorRank;
    private int mAuthorRet;
    public String mAuthorUserName;
    private int mClientRank;
    private boolean mIsAuthorizationed;
    public ParamCheck mParamCheck;
    private ScheduleTimer mScheduleTimer;
    UIHandler mUIHandler;
    private int mUnAuthorCount;

    public void initIntervalTimer() {
    }

    static /* synthetic */ int access$008(AuthorManager authorManager) {
        int i = authorManager.mUnAuthorCount;
        authorManager.mUnAuthorCount = i + 1;
        return i;
    }

    private void initClientRank() {
        this.mClientRank = 1;
        Log.debug(TAG, "init clent rank to " + this.mClientRank);
    }

    public int getClientRank() {
        return this.mClientRank;
    }

    public void clearAuthorInfo() {
        setIsAuthorizationed(false);
        this.mScheduleTimer.cancel();
    }

    public void setIsAuthorizationed(boolean z) {
        this.mIsAuthorizationed = z;
    }

    public boolean checkIsAuthorizationed() {
        Log.info(TAG, "authorization checked : " + this.mIsAuthorizationed);
        if (!this.mIsAuthorizationed) {
            boolean isPastAuthEmm = getIsPastAuthEmm();
            Log.info(TAG, "isPastAuthEmm : " + isPastAuthEmm);
            if (isPastAuthEmm) {
                initIntervalTimer();
                return false;
            }
            processCheckResult(-5);
            return false;
        }
        initIntervalTimer();
        return false;
    }

    private boolean getIsPastAuthEmm() {
        return TextUtils.equals(SangforAuth.getInstance().getPastAuthEmm(), "1");
    }

    public void setAuthorizationResult(AuthorResult authorResult) {
        setAuthorizationResult(authorResult.mRet, authorResult.mInterval, authorResult.mRank);
    }

    public void setAuthorizationResult(int i, int i2, int i3) {
        this.mAuthorRet = i;
        int i4 = i2 * 1000 * 60;
        if (i4 > 0) {
            long j = i4;
            if (this.mAuthorInterval != j) {
                this.mAuthorInterval = j;
                this.mScheduleTimer.changeTimerIntervalTime(0L, j);
            }
        }
        if (i3 > 0 && this.mAuthorRank != i3) {
            this.mAuthorRank = i3;
        }
        Log.info(TAG, "setAuthorizationResult: " + i + "|" + i4 + "|" + i3);
        setIsAuthorizationed(true);
    }

    public void processCheckResult() {
        processCheckResult(this.mAuthorRet);
    }

    public void processCheckResult(int i) {
        Message obtainMessage = this.mUIHandler.obtainMessage(0);
        obtainMessage.arg1 = i;
        this.mUIHandler.sendMessage(obtainMessage);
    }

    public int getAuthorizationRet() {
        return this.mAuthorRet;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public final class UIHandler extends Handler {
        public UIHandler(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (message.what == 0) {
                handleCheckRet(message.arg1);
            } else {
                Log.error(AuthorManager.TAG, "unknown message");
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:25:0x006e  */
        /* JADX WARN: Removed duplicated region for block: B:26:0x0087  */
        /* JADX WARN: Removed duplicated region for block: B:33:0x00bd  */
        /* JADX WARN: Removed duplicated region for block: B:35:? A[RETURN, SYNTHETIC] */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct code enable 'Show inconsistent code' option in preferences
        */
        public void handleCheckRet(int r7) {
            /*
                r6 = this;
                java.lang.StringBuilder r0 = new java.lang.StringBuilder
                r0.<init>()
                java.lang.String r1 = "handle author timer check "
                r0.append(r1)
                r0.append(r7)
                java.lang.String r0 = r0.toString()
                java.lang.String r1 = "AuthorManager"
                com.sangfor.bugreport.logger.Log.debug(r1, r0)
                com.sangfor.ssl.service.auth.AuthorManager r0 = com.sangfor.ssl.service.auth.AuthorManager.this
                java.lang.String r0 = r0.getAuthorErrorString(r7)
                r2 = -5
                r3 = 1
                r4 = 0
                if (r7 == r2) goto L64
                r2 = -4
                if (r7 == r2) goto L5a
                if (r7 == 0) goto L31
                if (r7 == r3) goto L2b
            L28:
                r7 = 0
                r2 = 0
                goto L6c
            L2b:
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.service.auth.AuthorManager.access$002(r7, r4)
                goto L69
            L31:
                com.sangfor.ssl.SangforAuthManager r7 = com.sangfor.ssl.SangforAuthManager.getInstance()
                com.sangfor.ssl.IConstants$VPNStatus r7 = r7.queryStatus()
                com.sangfor.ssl.service.auth.AuthorManager r2 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.service.auth.AuthorManager.access$008(r2)
                com.sangfor.ssl.IConstants$VPNStatus r2 = com.sangfor.ssl.IConstants.VPNStatus.VPNOFFLINE
                if (r7 == r2) goto L4c
                com.sangfor.ssl.service.auth.AuthorManager r2 = com.sangfor.ssl.service.auth.AuthorManager.this
                int r2 = com.sangfor.ssl.service.auth.AuthorManager.access$000(r2)
                r5 = 3
                if (r2 <= r5) goto L4c
                goto L28
            L4c:
                com.sangfor.ssl.IConstants$VPNStatus r2 = com.sangfor.ssl.IConstants.VPNStatus.VPNOFFLINE
                if (r7 != r2) goto L57
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.service.auth.AuthorManager.access$002(r7, r4)
                r7 = 1
                goto L6a
            L57:
                r7 = 0
                r2 = 1
                goto L6b
            L5a:
                java.lang.String r7 = "author timer check have network error"
                com.sangfor.bugreport.logger.Log.error(r1, r7)
                r7 = 0
                r2 = 0
                r3 = 0
                r4 = 1
                goto L6c
            L64:
                java.lang.String r7 = "do not authored"
                com.sangfor.bugreport.logger.Log.info(r1, r7)
            L69:
                r7 = 0
            L6a:
                r2 = 0
            L6b:
                r3 = 0
            L6c:
                if (r3 == 0) goto L87
                java.lang.String r7 = "author check error, need logout"
                com.sangfor.bugreport.logger.Log.error(r1, r7)
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.common.ScheduleTimer r7 = com.sangfor.ssl.service.auth.AuthorManager.access$100(r7)
                r7.cancel()
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.service.auth.AuthorManager.access$200(r7)
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.service.auth.AuthorManager.access$300(r7)
                goto Lb7
            L87:
                if (r4 == 0) goto La1
                java.lang.String r7 = "network restore, recover check interval"
                com.sangfor.bugreport.logger.Log.info(r1, r7)
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.common.ScheduleTimer r7 = com.sangfor.ssl.service.auth.AuthorManager.access$100(r7)
                r1 = 300000(0x493e0, double:1.482197E-318)
                com.sangfor.ssl.service.auth.AuthorManager r3 = com.sangfor.ssl.service.auth.AuthorManager.this
                long r3 = com.sangfor.ssl.service.auth.AuthorManager.access$400(r3)
                r7.changeTimerIntervalTime(r1, r3)
                goto Lb7
            La1:
                if (r7 != 0) goto La5
                if (r2 == 0) goto Lb7
            La5:
                com.sangfor.ssl.service.auth.AuthorManager r7 = com.sangfor.ssl.service.auth.AuthorManager.this
                com.sangfor.ssl.common.ScheduleTimer r7 = com.sangfor.ssl.service.auth.AuthorManager.access$100(r7)
                r1 = 60000(0xea60, double:2.9644E-319)
                com.sangfor.ssl.service.auth.AuthorManager r3 = com.sangfor.ssl.service.auth.AuthorManager.this
                long r3 = com.sangfor.ssl.service.auth.AuthorManager.access$400(r3)
                r7.changeTimerIntervalTime(r1, r3)
            Lb7:
                boolean r7 = android.text.TextUtils.isEmpty(r0)
                if (r7 != 0) goto Lc4
                com.sangfor.ssl.SangforAuth r7 = com.sangfor.ssl.SangforAuth.getInstance()
                r7.vpnSeterr(r0)
            Lc4:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.sangfor.ssl.service.auth.AuthorManager.UIHandler.handleCheckRet(int):void");
        }
    }

    public boolean updateSvpnEncKey() {
        String str = ((String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_KEY)) + PUBKEY_SVPN_FILE_UPDATE_URL;
        try {
            HttpRequest httpRequest = HttpRequest.get(str);
            if (str.startsWith(IGeneral.PROTO_HTTPS_HEAD)) {
                httpRequest.trustAllCerts();
                httpRequest.trustAllHosts();
            }
            if (httpRequest.ok()) {
                File file = new File(this.mAppContext.getFilesDir(), "emm_pubkey_svpn.enc.key.tmp");
                if (file.exists()) {
                    file.delete();
                }
                httpRequest.receive(file);
                file.setReadable(true);
                File file2 = new File(this.mAppContext.getFilesDir(), PUBKEY_SVPN_FILE);
                if (file2.exists()) {
                    file2.delete();
                }
                file.renameTo(file2);
                Log.info(TAG, "update author key success");
                return true;
            }
            return false;
        } catch (Exception e) {
            Log.error(TAG, e.getMessage());
            return false;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyLogout() {
        new Handler().postDelayed(new Runnable() { // from class: com.sangfor.ssl.service.auth.AuthorManager.1
            @Override // java.lang.Runnable
            public void run() {
                L3vpnController.getInstance().svpnLogout(LogoutTaskState.LogoutType.ACTIVE_LOGOUT);
            }
        }, 0L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void delegateAuthorErrorCallBack() {
        IVpnDelegate delegate = SangforAuth.getInstance().getDelegate();
        if (delegate == null) {
            Log.error(TAG, "get delegate is null, can not call back author check failed");
        } else {
            delegate.vpnCallback(-7, 10);
        }
    }

    public String getAuthorErrorString(int i) {
        switch (i) {
            case -7:
                return Values.strings.AUTHOR_TIMER_CHECK_FAKE_NET;
            case -6:
                return Values.strings.AUTHOR_TIMER_CHECK_RANDOM;
            case -5:
            case -4:
            case 1:
                return "";
            case -3:
                return Values.strings.AUTHOR_TIMER_CHECK_PARAERROR;
            case -2:
                return Values.strings.AUTHOR_TIMER_CHECK_RANK_ERROR;
            case -1:
                return Values.strings.AUTHOR_TIMER_CHECK_ERROR;
            case 0:
                return Values.strings.AUTHOR_TIMER_CHECK_ERROR;
            case 2:
                return Values.strings.AUTHOR_TIMER_CHECK_FREIZE;
            default:
                return Values.strings.AUTHOR_TIMER_CHECK_UNKONW;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void easyAppTimerCheck() {
        AuthNativesManager.getInstance().nRequetAuthorCheck();
    }

    public int getAuthorRank() {
        return this.mClientRank;
    }

    private void addFrontSwitchCallbacks() {
        Foreground.get().addListener(this.foregroundListener);
    }

    public int doParamCheck(ParamCheck paramCheck, int i) {
        if (this.mParamCheck.isNeedCheck) {
            this.mParamCheck.isNeedCheck = false;
            if (i != 1 || this.mParamCheck.mRank <= paramCheck.mRank) {
                if (this.mParamCheck.mRandom != paramCheck.mRandom) {
                    Log.error(TAG, "author check random error");
                    return -6;
                }
                return i;
            }
            return -2;
        }
        return i;
    }

    public boolean isServerConnect(int i) {
        boolean z = false;
        try {
            String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_KEY);
            HttpRequest httpRequest = HttpRequest.get(str);
            if (str.startsWith(IGeneral.PROTO_HTTPS_HEAD)) {
                httpRequest.trustAllCerts();
                httpRequest.trustAllHosts();
            }
            httpRequest.connectTimeout(i);
            z = httpRequest.ok();
            Log.debug(TAG, "detect vpn url " + str + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + httpRequest.code());
            return z;
        } catch (Exception e) {
            Log.error(TAG, "check connect server error:" + e.getMessage());
            return z;
        }
    }

    private AuthorManager() {
        this.mUnAuthorCount = 0;
        this.mIsAuthorizationed = false;
        this.mAuthorInterval = 3600000L;
        this.SCHEDULE_TIMERTASK = new ScheduleTimer.ScheduleTimerTask() { // from class: com.sangfor.ssl.service.auth.AuthorManager.2
            @Override // com.sangfor.ssl.common.ScheduleTimer.ScheduleTimerTask
            public void run() {
                Log.info(AuthorManager.TAG, "ScheduleTimerTask start");
                AuthorManager.this.easyAppTimerCheck();
            }
        };
        this.foregroundListener = new Foreground.Listener() { // from class: com.sangfor.ssl.service.auth.AuthorManager.3
            @Override // com.sangfor.ssl.common.Foreground.Listener
            public void onBecameForeground() {
                AuthorManager.this.mScheduleTimer.resume();
            }

            @Override // com.sangfor.ssl.common.Foreground.Listener
            public void onBecameBackground() {
                AuthorManager.this.mScheduleTimer.pause();
            }
        };
        this.mUIHandler = new UIHandler(Looper.getMainLooper());
        this.mScheduleTimer = new ScheduleTimer();
        this.mParamCheck = new ParamCheck();
        this.mAppContext = SangforAuth.getInstance().getContext();
        initClientRank();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static final class SingletonHolder {
        private static final AuthorManager INSTANCE = new AuthorManager();

        private SingletonHolder() {
        }
    }

    public static final AuthorManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /* loaded from: classes2.dex */
    static class AuthorResult {
        int mInterval;
        int mRank;
        int mRet;

        public AuthorResult(int i, int i2, int i3) {
            this.mRet = i;
            this.mInterval = i2;
            this.mRank = i3;
        }
    }

    /* loaded from: classes2.dex */
    static class ParamCheck {
        boolean isNeedCheck = false;
        int mRandom;
        int mRank;

        public ParamCheck() {
        }

        public ParamCheck(int i, int i2) {
            this.mRandom = i;
            this.mRank = i2;
        }

        public void setCheckInfo(int i, int i2) {
            this.mRandom = i;
            this.mRank = i2;
            this.isNeedCheck = true;
        }
    }
}
